package javafx;

import javafx.application.Application;
import javafx.collections.ListChangeListener;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;

public class main extends Application {

	public static void main(String[] args) {
		launch(args);
	}

	@Override
	public void start(Stage stage) throws Exception {

		Button button1 = new Button("b1");
		Button button2 = new Button("b2");
		Button button3 = new Button("b3");

		button1.setLayoutX(10);
		button1.setLayoutY(10);

		button2.setLayoutX(50);
		button2.setLayoutY(40);

		button3.setLayoutX(10);
		button3.setLayoutY(70);

		Group group = new Group();
		group.getChildren().addAll(button1, button2, button3);

		// group监听器
		group.getChildren().addListener(new ListChangeListener<Node>() {

			@Override
			public void onChanged(Change<? extends Node> c) {

				System.out.println("当前组件数量= " + c.getList().size());

			}

		});

		// 给按钮添加一个单机事件
		button1.setOnAction(new EventHandler<ActionEvent>() {

			@Override
			public void handle(ActionEvent event) {

				Button button4 = new Button("b4");
				group.getChildren().add(button4);

			}
		});

		Scene scene = new Scene(group);

		stage.setScene(scene);
		stage.setTitle("javaFX");
		stage.setWidth(600);
		stage.setHeight(600);

		stage.show();

	}

}
